package com.daiyang.campus_lucky_draw.common.interceptor;


import com.daiyang.campus_lucky_draw.common.utils.JWTUtil;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //从header获取token
        String jwtToken=request.getHeader("user_token");
        log.info("获取路径:{}", request.getRequestURI());
        log.info("从header中获取token:{}", jwtToken);

        //验证用户 token
        Claims claims = JWTUtil.parseJWT(jwtToken);
        if(claims==null){
            log.info("验证不通过");
            response.setStatus(401);
            return false;
        } else {
            log.info("令牌验证通过, 放⾏");
            return true;
        }
    }
}
